<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="校区" prop="shopId">
      <el-select v-model="dataForm.shopId" placeholder= "校区">
        <el-option v-for="item in shopList"
                   :label="item.shopName"
                   :key="item.id"
                   :value="item.id"
        ></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="配合人" prop="partner">
      <el-select v-model="dataForm.partner" placeholder="配合人">
        <el-option v-for="(item, index) in partnerPtions" :label="item.label" :key="item.value" :value="item.value"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="分红比例" prop="moneyProportion">
      <el-input v-model="dataForm.moneyProportion" placeholder="分红比例(请输入百分比,例如:20%)"></el-input>
    </el-form-item>
    <el-form-item label="提示信息" prop="partnerName">
      <el-input v-model="dataForm.partnerName" placeholder="提示信息"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          businessId: '',
          shopId: '',
          partner: '',
          moneyProportion: '',
          partnerName: ''
        },
        shopList: [],
        partnerPtions: [{
          value: '1',
          label: '邀约员'
        }, {
          value: '2',
          label: '体验员'
        }, {
          value: '3',
          label: '跟进员'
        }],
        dataRule: {
          shopId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          partner: [
            { required: true, message: '配合人不能为空', trigger: 'blur' }
          ],
          moneyProportion: [
            { required: true, message: '分红比例不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/business/partnerType/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.businessId = data.partnerType.businessId
                this.dataForm.shopId = data.partnerType.shopId
                this.dataForm.partner = data.partnerType.partner.toString()
                this.dataForm.moneyProportion = data.partnerType.moneyProportion
                this.dataForm.partnerName = data.partnerType.partnerName
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/business/partnerType/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'businessId': this.dataForm.businessId,
                'shopId': this.dataForm.shopId,
                'partner': this.dataForm.partner,
                'moneyProportion': this.dataForm.moneyProportion,
                'partnerName': this.dataForm.partnerName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      // 获取机构下所有店铺信息
      getShopInfo () {
        this.$http({
          url: this.$http.adornUrl('/business/student/getShopInfo'),
          mothod: 'get',
          params: {'shopId': ''}
        }).then(({data}) => {
          this.shopList = data.list
        })
      }
    }
  }
</script>
